package com.leyou.user.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.leyou.common.constants.MQConstants.Exchange.SMS_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.RoutingKey.VERIFY_CODE_KEY;

/**
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
@Service
@Transactional
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    private static final String MSG_CHECK_CORE_PRE = "MSG_CHECK_CORE_PRE ";

    public Boolean checkUsernameAndPhone(String data, Integer type) {
     try{
         User record = new User();
         switch (type) {
             case 1:
                 record.setUsername(data);
                 break;
             case 2:
                 record.setPhone(data);
                 break;
             default:
                 throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
         }
         int count = userMapper.selectCount(record);
         //如果没有查询到返回ture
         return count == 0;
     }catch(Exception e){
         throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
     }
    }
    public void sendCheckCode(String phone) {
        //生成短信验证码
        String code = RandomStringUtils.randomNumeric(6);
        //把短信验证码存入到redis中
        redisTemplate.opsForValue().set(MSG_CHECK_CORE_PRE+phone,code,2, TimeUnit.HOURS);
        //封装发送的MQ消息内容
        Map<String, String> checkCodeMap = new HashMap<>();
        checkCodeMap.put("phone", phone);
        checkCodeMap.put("code", code);
        //向指定队列中发送MQ消息
        amqpTemplate.convertAndSend(SMS_EXCHANGE_NAME, VERIFY_CODE_KEY, checkCodeMap);

    }

    public void register(User user, String code,String confirmPassword) {
        try{
            //验证验证码是否正确
            String redisCode = redisTemplate.opsForValue().get(MSG_CHECK_CORE_PRE + user.getPhone());
            if (!StringUtils.equals(code, redisCode)) {
                throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
            }
            //验证两个密码是否一致
            if (!StringUtils.equals(confirmPassword, user.getPassword())) {
                throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
            }
            //对密码进行加密
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            //保存数据
            userMapper.insertSelective(user);
        }catch(Exception e){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
    }
}